<template>
	<div class="addVideo">
		<el-dialog
			:title="`${type == 'add' ? '新增' : '编辑'}城市`"
			:visible.sync="dialogVisible"
			width="550px"
			:modal-append-to-body="false"
			:close-on-click-modal="false"
			custom-class="custom-dialog"
		>
			<el-form
				ref="forms"
				size="small"
				:model="forms"
				:rules="rules"
				label-width="100px"
				label-position="right"
			>
				<el-form-item label="城市名称：" prop="city_name">
					<el-input
						v-model="forms.city_name"
						placeholder="请输入城市名称"
						clearable
						maxlength="20"
						show-word-limit
						style="width: 100%"
					/>
				</el-form-item>
				<el-form-item label="城市地区：" prop="city_region">
					<el-input
						v-model="forms.city_region"
						placeholder="请输入城市地区"
						clearable
						style="width: 100%"
					/>
				</el-form-item>
				<el-form-item label="是否禁用：" prop="is_close">
					<el-select v-model="forms.is_close" placeholder="请选择">
						<el-option label="是" value="1" />
						<el-option label="否" value="0" />
					</el-select>
				</el-form-item>
				<el-form-item prop="city_img" label="城市配图：">
					<UploadImage ref="upload" :maxSize="2" @files="onUpload" :files="fileList" />
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button style="margin-right: 5px" @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="saveVideos">保存</el-button>
			</span>
		</el-dialog>
	</div>
</template>
<script>
	import { mapGetters } from 'vuex'
	import Editor from '@/components/Editor'
	import { cityEdit, cityAdd, cityDetail } from '@/api/competition-manage.js'
	import UploadImage from '@/components/UploadHttp/UploadImage.vue'
	import { uploadFunc } from '@/utils/upload.js'
	export default {
		name: 'add-daily',
		components: { Editor, UploadImage },
		props: {},
		data() {
			return {
				dialogVisible: false,
				disabled: false,
				forms: {
					title: '',
					content: ''
				},
				rules: {
					city_name: [{ required: true, message: '请输入城市名称', trigger: 'blur' }],
					pic: [{ required: true, message: '请选择文章缩略图', trigger: 'blur' }],
					city_region: [{ required: true, message: '请输入城市地区', trigger: 'change' }]
				},
				type: 'add',
				fileList: []
			}
		},
		computed: {
			...mapGetters(['token', 'userInfo'])
		},
		methods: {
			initData(type, info) {
				this.type = type
				this.forms = {
					city_name: '',
					city_img: '',
					city_region: '',
					is_close: '0',
					city_time: '0' //缓存时间
				}
				this.fileList = []
				if (type === 'edit') {
					this.getDetailInfo(info.city_id)
				}
				this.dialogVisible = true
				this.$nextTick(() => {
					this.$refs.forms.clearValidate()
				})
			},
			async getDetailInfo(info_id) {
				const loading = this.$setLoading()
				try {
					const res = await cityDetail({ city_id: info_id })
					this.forms = {
						city_id: res.data.city_id,
						city_name: res.data.city_name,
						city_img: res.data.city_img,
						city_region: res.data.city_region,
						is_close: res.data.is_close || '0',
						city_time: res.data.city_time || '0' //缓存时间
					}
					this.fileList = res.data.city_img ? [{ url: res.data.city_img }] : []
					loading.close()
				} catch (e) {
					loading.close()
					console.log(e)
				}
			},
			// 保存
			async saveVideos() {
				this.$refs.forms.validate(async valid => {
					if (valid) {
						const loading = this.$loading({
							lock: true,
							text: '数据提交中~'
						})
						// 判断有没有需要上传的文件
						const uploadList = this.fileList.filter(item => {
							return item.status === 'ready' && item.raw
						})
						if (!uploadList.length) {
							this.saveData(loading)
						} else {
							uploadFunc(this.fileList).then(res => {
								this.saveData(loading)
							})
						}
					}
				})
			},
			// 保存更新进度数据
			async saveData(loading) {
				const { forms, fileList } = this
				forms.city_img = fileList.map(item => item.url).join(',')
				if (this.type === 'add') {
					cityAdd(forms).then(
						res => {
							this.$message.success('新增成功！')
							this.dialogVisible = false
							this.$parent.loadData()
							loading.close()
						},
						err => {
							loading.close()
						}
					)
				} else {
					cityEdit(forms).then(
						res => {
							this.$message.success('修改成功！')
							this.dialogVisible = false
							this.$parent.loadData()
							loading.close()
						},
						err => {
							loading.close()
						}
					)
				}
			},
			onUpload(list) {
				this.fileList = list
				this.forms.pic = list.length ? list[0].url : ''
				this.$refs.forms.validateField('pic')
				console.log(list)
			}
		}
	}
</script>
<style lang="scss" scoped>
	.addVideo {
		::v-deep {
			.el-dialog .el-dialog__body {
				padding-bottom: 20px;
			}
			.picBox .upload-img,
			.picBox {
				width: 120px !important;
				height: 120px !important;
				line-height: 120px;
			}
		}
		.box-wrap {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
		}
	}
</style>
